import time,random

from selenium import  webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver import ActionChains, Keys
def get_driver():
    driver = webdriver.Edge()
    driver.implicitly_wait(5)
    driver.maximize_window()
    return driver
def input(element,value):
    element.click()
    element.clear()
    element.send_keys(value)
def select_random_first(element):
    options_count = len(Select(element).options)
    random_index = random.randint(1, options_count-1)
    Select(element).select_by_index(random_index)


if __name__ == '__main__':
    driver = get_driver()
    driver.get('http://localhost:8080/YunDuanBoss/login/')
    uname = driver.find_element('name','userName')
    input(uname,'YDHR000')
    upass = driver.find_element('name','userPass')
    input(upass,'yunduan123')
    driver.find_element('css selector','#form-login > div > div > div.modal-footer > button').click()
    time.sleep(3)

    driver.get('http://localhost:8080/YunDuanBoss/assets')
    driver.find_element('css selector','#content > div.row.con-margin.con-body-con > div:nth-child(1) > div > button:nth-child(1)').click()
    #随机设备名称
    assert_name = driver.find_element('css selector','#addAss-form > div > div > div:nth-child(1) > div:nth-child(1) > select')
    select_random_first(assert_name)
    # 随机设备型号
    assert_type = driver.find_element('css selector','#addAss-form > div > div > div:nth-child(1) > div:nth-child(2) > span:nth-child(2) > select')
    select_random_first(assert_type)
    # 输入合法条码
    barcode = driver.find_element('css selector','#addAss-form > div > div > div:nth-child(2) > div:nth-child(1) > input')
    input(barcode,'12354885452')
    # 采购价
    price = driver.find_element('css selector','#addAss-form > div > div > div:nth-child(2) > div.col-md-6.col-sm-6.form-group')
    input(price,'800')
    # 归属人
    purchase_employee =  driver.find_element('css selector','#addAss-form > div > div > div:nth-child(3) > div:nth-child(1) > input.text.autoEmpId')
    input( purchase_employee,'李艳')
    time.sleep(2)
    ActionChains(driver).send_keys(Keys.DOWN).perform()
    time.sleep(2)
    ActionChains(driver).send_keys(Keys.ENTER).perform()
    time.sleep(2)
    driver.find_element('css selector','body > div:nth-child(21) > div.datetimepicker-days > table > tfoot > tr:nth-child(1) > th').click()
    time.sleep(2)
    driver.find_element('css selector','#addAss-form > div > div > div:nth-child(4) > div:nth-child(1) > input')
    driver.find_element('css selector','#addAssBtn')